package com.jt.tenant.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.module.round.RoundedImageView;
import com.jt.tenant.R;
import com.jt.tenant.activity.AbnoramlInfoListActivity;
import com.jt.tenant.activity.AboutSoftwareActivity;
import com.jt.tenant.activity.EquipSettingActivity;
import com.jt.tenant.activity.InfoSettingActivity;
import com.jt.tenant.activity.IntegralActivity;
import com.jt.tenant.activity.PersonInfoActivity;
import com.jt.tenant.activity.PwdChangeActivity;
import com.jt.tenant.app.JTApplication;
import com.jt.tenant.dialog.TipDialog;
import com.zcx.helper.activity.AppV4Fragment;
import com.zcx.helper.util.UtilToast;

/**
 * Created by AD on 2016/5/12.
 */
public class MeFragment extends AppV4Fragment implements OnClickListener {

    private RoundedImageView head_img;

    private TextView nickname,phone,integral1,integral2;

    private LinearLayout lin_me_1,lin_me_2,lin_me_3,lin_me_4,lin_me_5,lin_me_6,lin_me_7,lin_me_8;

    private ImageView iv_me_1,iv_me_2,iv_me_3,iv_me_4,iv_me_5,iv_me_6,iv_me_7,iv_me_8;

    private TipDialog quiteDialog;

    public static RefreshLinster refreshLinster;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = JTApplication.ScaleScreenHelper.loadView((ViewGroup) inflater.inflate(R.layout.fragment_me, null));

        initView(view);

        initData();

        return view;

    }


    private void initView(View view) {

        head_img = (RoundedImageView)view.findViewById(R.id.iv_head_img);

        nickname = (TextView)view.findViewById(R.id.tv_me_nickname) ;

        phone = (TextView)view.findViewById(R.id.tv_me_phone) ;

        integral1 = (TextView)view.findViewById(R.id.tv_me_integral1) ;

        integral2=  (TextView)view.findViewById(R.id.tv_me_integral2) ;

        (lin_me_1 = (LinearLayout)view.findViewById(R.id.lin_me_1)).setOnClickListener(this);

        (lin_me_2 = (LinearLayout)view.findViewById(R.id.lin_me_2)).setOnClickListener(this);

        (lin_me_3 = (LinearLayout)view.findViewById(R.id.lin_me_3)).setOnClickListener(this);

        (lin_me_4 = (LinearLayout)view.findViewById(R.id.lin_me_4)).setOnClickListener(this);

        (lin_me_5 = (LinearLayout)view.findViewById(R.id.lin_me_5)).setOnClickListener(this);

        (lin_me_6 = (LinearLayout)view.findViewById(R.id.lin_me_6)).setOnClickListener(this);

        (lin_me_7 = (LinearLayout)view.findViewById(R.id.lin_me_7)).setOnClickListener(this);

        (lin_me_8 = (LinearLayout)view.findViewById(R.id.lin_me_8)).setOnClickListener(this);

        iv_me_1 = (ImageView)view.findViewById(R.id.iv_me_1);

        iv_me_2 = (ImageView)view.findViewById(R.id.iv_me_2);

        iv_me_3 = (ImageView)view.findViewById(R.id.iv_me_3);

        iv_me_4 = (ImageView)view.findViewById(R.id.iv_me_4);

        iv_me_5 = (ImageView)view.findViewById(R.id.iv_me_5);

        iv_me_6 = (ImageView)view.findViewById(R.id.iv_me_6);

        iv_me_7 = (ImageView)view.findViewById(R.id.iv_me_7);

        iv_me_8 = (ImageView)view.findViewById(R.id.iv_me_8);

        if(!JTApplication.jtPreferences.readAccountState().equals("2")){

            iv_me_4.setImageResource(R.mipmap.icon_me_abnormal_off);

        }

        refreshLinster = new RefreshLinster() {
            @Override
            public void refresh() {

                initData();

            }
        };

    }

    private void initData(){

        JTApplication.UtilAsyncBitmap.get(JTApplication.jtPreferences.readHeadPath(), head_img, R.mipmap.iv_face);

        nickname.setText(JTApplication.jtPreferences.readNickName());

        phone.setText(JTApplication.jtPreferences.readPhone());

        integral1.setText(JTApplication.jtPreferences.readUserIntegral("2"));

        integral2.setText(JTApplication.jtPreferences.readUserIntegral("1"));

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.lin_me_1:

                startActivity(new Intent(getActivity(), PersonInfoActivity.class));

                break;

            case R.id.lin_me_2:

                startActivity(new Intent(getActivity(), EquipSettingActivity.class));

                break;

            case R.id.lin_me_3:

                startActivity(new Intent(getActivity(), PwdChangeActivity.class));

                break;

            case R.id.lin_me_4:

                if(JTApplication.jtPreferences.readAccountState().equals("2")){

                    startActivity(new Intent(getActivity(), AbnoramlInfoListActivity.class));

                }else {

                    UtilToast.show(getContext(), "该功能需要绑定OBD设备");

                }

                break;

            case R.id.lin_me_5:

                startActivity(new Intent(getActivity(), InfoSettingActivity.class));

                break;

            case R.id.lin_me_6:

                startActivity(new Intent(getActivity(), IntegralActivity.class));

                break;

            case R.id.lin_me_7:

                startActivity(new Intent(getActivity(), AboutSoftwareActivity.class));

                break;

            case R.id.lin_me_8:

                quiteDialog = new TipDialog(getActivity());

                quiteDialog.setTip("您确定要退出登录么？");

                quiteDialog.setType(2);

                quiteDialog.show();

                break;

        }

    }

    public abstract class RefreshLinster {

        public abstract void refresh();

    }

}
